<?php
/**
 * created by zzy
 * date: 2018/1/26 14:07
 */


namespace App\Api\Service\Logistics\Controllers;

use App\Eloquent\Ygt\DepartmentUser;
use App\Eloquent\Ygt\LogisticsLine;
use Framework\BaseClass\Api\Controller;
use App\Engine\Func;

class LineController extends Controller
{
    public function index(){}
    public function lists()
    {
        $userId             = Func::getHeaderValueByName('userid');
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $logisticsId        = request('id',0);
        if( !$userInfo || !$logisticsId ){
            xThrow( ERR_PARAMETER );
        }
        $companyId          = $userInfo->company_id;
        $where              = ['company_id'=>$companyId,'logistics_id'=>$logisticsId];
        $collection         = LogisticsLine::getList($where);
        $result             = $collection->toArray();
        return $result;
    }
    public function edit()
    {
        $userId             = Func::getHeaderValueByName('userid',1);
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        if( !$userInfo ){
            xThrow( ERR_PARAMETER );
        }
        $companyId          = $userInfo->company_id;
        $isSubmit           = request('is_submit',0);
        $lineId             = request('id',0);
        $logisticsId        = request('logistics_id',0);
        if($isSubmit){
            $data                   = request()->all();
            $data['company_id']   = $companyId;
            if($lineId==0)
            {
                $data['logistics_id']   = $logisticsId;
            }
            $fieldList              = LogisticsLine::$tableFieldList;
            $tableData              = LogisticsLine::setDataByTableField($data,$fieldList);
            $tableDataCheckResult   = LogisticsLine::checkDataByTableField($tableData,$fieldList);
            if($tableDataCheckResult !== true)
            {
                xThrow(ERR_PARAMETER, $tableDataCheckResult);
            }
            $editResult             = LogisticsLine::edit($tableData);
            if($editResult===false)
            {
                xThrow(ERR_UNKNOWN);
            }
            $result         = ['message'=>'编辑成功'];
            return $result;
        } else {
            $where          = ['id'=>$lineId];
            $infoCollection = LogisticsLine::getInfo($where);
            $result         = [];
            if($infoCollection)
            {
                $result     = $infoCollection->toArray();
            }
            return $result;
        }
    }
    public function del()
    {
        $userId             = Func::getHeaderValueByName('userid');
        $userInfo           = DepartmentUser::getCurrentInfo($userId);
        $lineId             = request('id',0);
        if( !$userInfo || !$lineId ){
            xThrow( ERR_PARAMETER );
        }
        $companyId          = $userInfo->company_id;
        $where              = ['company_id'=>$companyId,'id'=>$lineId];
        $delResult          = LogisticsLine::del($where);
        if($delResult){
            $result         = ['message'=>'删除成功'];
        }else{
            $result         = ['message'=>'删除失败'];
        }
        return $result;
    }
}